package com.cn.lycss.dao.impl;

import com.cn.lycss.dao.ProductDao;
import com.cn.lycss.model.PageModel;
import com.cn.lycss.model.ProductModel;
import com.cn.lycss.model.UserModel;
import com.cn.lycss.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> findAll(ProductModel model) {
        String sql = " select p.*,c.name  cuisineName from product p\n" +
                "inner join cuisine c\n"+
                " on p.cuisine_id=c.id  order by id desc";
        sql += " limit " + (model.getPageNumber() - 1) * model.getPageSize() + "," + model.getPageSize() + "";
        ResultSet resultSet= JDBC.query(sql);
        List<ProductModel> list=new ArrayList<>();
//        try {
//            while (resultSet.next()){
//                model.setId(resultSet.getInt("id"));
//                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
//                model.setName(resultSet.getString("name"));
//                model.setImgHref(resultSet.getString("img_href"));
//                model.setCreateTime(resultSet.getString("create_time"));
//                model.setUpdateTime(resultSet.getString("update_time"));
//                model.setEnable(resultSet.getInt("enable"));
//                model.setRemake(resultSet.getString("remark"));
//                model.setCuisineName(resultSet.getString("cuisineName"));
//                list.add(model);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

        return getList(sql);
    }


    public Integer add(ProductModel productModel) {
        return null;
    }

    @Override
    public int getCount(ProductModel model) {
     String sql = " select count(*) count  from product p\n" +
             "inner join cuisine c\n"+
             " on p.cuisine_id=c.id  ";
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                return resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public ProductModel findById(ProductModel productModel) {
        return null;
    }


    public Integer update(ProductModel productModel) {
        return null;
    }


    public Integer del(ProductModel productModel) {
        return null;
    }


    public Integer enable(ProductModel productModel) {
        return null;
    }

    private List<ProductModel> getList(String sql) {
        ResultSet resultSet = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();

                model.setId(resultSet.getInt("id"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setName(resultSet.getString("name"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setUpdateTime(resultSet.getString("update_time"));
                model.setEnable(resultSet.getInt("enable"));
                model.setRemake(resultSet.getString("remark"));
                model.setIsRecommend(resultSet.getInt("is_recommend"));
                model.setCuisineName(resultSet.getString("cuisineName"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setMarketPrice(resultSet.getBigDecimal("price"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
public static void main(String[] args) {
 ProductModel userModel=new ProductModel();
//    userModel.setPageNumber(1);
//    userModel.setPageSize(10);
    new ProductImpl().findAll(userModel);
}
}

